//
//  MBSpaceSystemNewsCell.m
//  MiBaoTu
//
//  Created by admin on 16/9/20.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceSystemNewsCell.h"

@interface MBSpaceSystemNewsCell () {
    UIView                  *_bgView;
    UILabel                 *_nickNameLb;
    UILabel                 *_timeLb;
    UIButton                *_selectBt;
    UIView                  *_contentBigView;
    UIView                  *_contentView;
    UILabel                 *_contentNameLb;
}

@property (nonatomic,assign)BOOL isCompliling;   //是否编译
@property (nonatomic,assign)BOOL isALlSelecting; //是否全选


@end
@implementation MBSpaceSystemNewsCell

- (void)awakeFromNib {
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    self.backgroundColor = [UIColor clearColor];
    self.backgroundView = nil;
    
    //背景
    _bgView = [[UIView alloc]initWithFrame:CGRectZero];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];

    //勾选按钮
    _selectBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectBt.backgroundColor = [UIColor clearColor];
    _selectBt.hidden = YES;
    [_selectBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    [_selectBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateHighlighted];
    [_selectBt addTarget:self action:@selector(onGouXuanToSelectAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_selectBt];
    
    //**************************
    //大模块 包含音乐标题、大小、类型以及下载图标
    _contentBigView = [[UIView alloc]initWithFrame:CGRectZero];
    _contentBigView.backgroundColor = [UIColor whiteColor];
    [_bgView addSubview:_contentBigView];
    
    //昵称
    _nickNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    _nickNameLb.textColor = UIColorFromHex(0x333333);
    _nickNameLb.font = [UIFont systemFontOfSize:13.f];
    [_contentBigView addSubview:_nickNameLb];
    
    //时间
    _timeLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _timeLb.backgroundColor = [UIColor clearColor];
    _timeLb.textAlignment = NSTextAlignmentLeft;
    _timeLb.textColor = UIColorFromHex(0x999999);
    _timeLb.font = [UIFont systemFontOfSize:11.f];
    [_contentBigView addSubview:_timeLb];
    
    //内容
    _contentView = [[UIView alloc]initWithFrame:CGRectZero];
    _contentView.backgroundColor = UIColorFromHex(0xeeeeee);
    [_contentBigView addSubview:_contentView];
    
    //内容标题
    _contentNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _contentNameLb.backgroundColor = [UIColor clearColor];
    _contentNameLb.textAlignment = NSTextAlignmentLeft;
    _contentNameLb.textColor = UIColorFromHex(0x333333);
    _contentNameLb.font = [UIFont systemFontOfSize:13.f];
    [_contentView addSubview:_contentNameLb];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    [self setNeedsLayout];
}

- (void)setDataDic:(NSDictionary *)dataDic
{
    _dataDic = dataDic;
    [self setNeedsLayout];
}

//勾选
- (void)onGouXuanToSelectAction:(UIButton *)bt
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectCtrlForGouXuanWithValue:WithSelect:WithView:)]) {
        bt.selected = !bt.selected;
        if (_dataDic) {
            [_delegate onSelectCtrlForGouXuanWithValue:_dataDic WithSelect:bt.selected WithView:self];
        }
    }
}

- (void)onCompileCellForLayout:(BOOL)isCompile WithAllSelct:(BOOL)isAllSelct
{
    self.isCompliling = isCompile;
    self.isALlSelecting = isAllSelct;
    [self setNeedsLayout];
}

- (void)setIsCompliling:(BOOL)isCompliling
{
    _isCompliling = isCompliling;
    [self setNeedsLayout];
}

- (void)setIsALlSelecting:(BOOL)isALlSelecting
{
    _isALlSelecting = isALlSelecting;
    [self setNeedsLayout];
}
#pragma make---转码
- (NSString *)replaceUnicode:(NSString *)unicodeStr {
    //张三  \u5f20\u4e09
    
    NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
                                                           mutabilityOption:NSPropertyListImmutable
                                                                     format:NULL
                                                           errorDescription:NULL];
    
    //  NSLog(@"Output = %@", returnStr);
    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    //value
    UIImage *selectNoImg = [UIImage imageNamed:@"weigouxuan_fb"];
    UIImage *selectImg = [UIImage imageNamed:@"xuanzhong_zl"];
    [_selectBt setImage:selectNoImg forState:UIControlStateNormal];
    [_selectBt setImage:selectImg forState:UIControlStateSelected];
    _selectBt.selected = self.isALlSelecting;
    
    _nickNameLb.text = @"系统消息";
    _timeLb.text = [[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"CreateTime"]] getChaTime];
    _contentNameLb.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"SysContent"]];
    
    //frame
    _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 177*PERCEN);
    _selectBt.frame = CGRectMake((ISIPAD ?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), (_bgView.height-selectNoImg.size.height)/2., selectNoImg.size.width, selectNoImg.size.height);
    _selectBt.hidden = !_isCompliling;
    
    _contentBigView.frame = CGRectMake((_selectBt.isHidden ? 0 : _selectBt.right+5), 0, _bgView.width, _bgView.height);
    
    _nickNameLb.frame = CGRectMake((ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), (WIDTH_5S ? 14*PERCEN : 20*PERCEN), 200, 20);

    _timeLb.frame = CGRectMake(_nickNameLb.left, _nickNameLb.bottom-(WIDTH_5S ? 5 : 2), 200, 20);
    
    _contentView.frame = CGRectMake(_timeLb.left, _timeLb.bottom+(WIDTH_5S ? 4*PERCEN : 12*PERCEN), (ISIPAD?_bgView.width-920*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):_bgView.width-50*PERCEN), 60*PERCEN);
    _contentNameLb.frame = CGRectMake(10, 0, _contentView.width-20, _contentView.height);
}

@end
